package com.pm.controller.caveatConf;

import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.caveatConf.CaveatConf;
import com.pm.service.caveatConf.CaveatConfService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "前后名次")
@UserLoginToken
@RestController
@RequestMapping("/caveatConf")
public class CaveatConfController {

    @Autowired
    CaveatConfService caveatConfService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增前后名次")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(CaveatConf caveatConf) {
        Response<Integer> response = null;
        // 大值比小值小
        if (caveatConf.getLe() < caveatConf.getGt()) {
            return new Response<>(ExpCodeState.caveat_conf_val_error);
        }
        // 不能新增返回false，能新增返回true
        Boolean b = caveatConfService.canAdd(caveatConf);
        if (!b) {
            return new Response<>(ExpCodeState.caveat_conf_error);
        }
        Integer result = caveatConfService.add(caveatConf);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除前后名次")
    public Response<Integer> remove(CaveatConf caveatConf) {
        Response<Integer> response = null;
        Integer result = caveatConfService.remove(caveatConf);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<CaveatConf> load(CaveatConf caveatConf) {
        Response<CaveatConf> response = null;
        CaveatConf result = caveatConfService.load(caveatConf);
        response = new Response<>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<CaveatConf>> getListAll(CaveatConf caveatConf) {
        List<CaveatConf> result = caveatConfService.getListAll(caveatConf);
        Response<List<CaveatConf>> res = new Response<List<CaveatConf>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("修改前后名次")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "修改前后名次")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(CaveatConf caveatConf) {
        Response<Integer> response = null;
        // 不能修改返回false，能修改返回true
        Boolean b = caveatConfService.canAdd(caveatConf);
        if (!b) {
            return new Response<>(ExpCodeState.caveat_conf_error);
        }
        Integer result = caveatConfService.update(caveatConf);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }
}
